library(readr)
library(car)
library(tidyverse)
data <- read_rds("sk_pobe_repdata.rds")

#data$age<-as.numeric(data$age)
#table(data$age)

data$sasian<-data$participant.sasian

data$easian<-ifelse(data$participant.ntlrace=='Chinese'|
                      data$participant.ntlrace=='Japanese'|
                      data$participant.ntlrace=='Korean'|
                      data$participant.ntlrace=='Taiwanese'|
                      data$participant.ntlrace=='Mongolian',1,0)

data$seasian<-ifelse(data$participant.ntlrace=='Burmese'|
                       data$participant.ntlrace=='Cambodian'|
                       data$participant.ntlrace=='Filipino/a'|
                       data$participant.ntlrace=='Hmong'|
                       data$participant.ntlrace=='Indonesian'|
                       data$participant.ntlrace=='Laotian'|
                       data$participant.ntlrace=='Malaysian'|
                       data$participant.ntlrace=='Singaporean'|  
                       data$participant.ntlrace=='Thai'|
                       data$participant.ntlrace=='Vietnamese',1,0)

data$chinese<-ifelse(data$participant.ntlrace=='Chinese',1,0)
data$indian<-ifelse(data$participant.ntlrace=='Indian',1,0)
data$vietnamese<-ifelse(data$participant.ntlrace=='Vietnamese',1,0)
data$korean<-ifelse(data$participant.ntlrace=='Korean',1,0)
data$filipino<-ifelse(data$participant.ntlrace=='Filipino/a',1,0)
data$japanese<-ifelse(data$participant.ntlrace=='Japanese',1,0)
data$allothers<-ifelse(data$chinese==0&
                         data$indian==0&
                         data$vietnamese==0&
                         data$korean==0&
                         data$filipino==0&
                         data$japanese==0,1,0)

#table(data$instruction.1.player.cq1_correct)
data$asianid1 <- car::recode(data$prequestionnaire.1.player.panid1, "1=4;2=3;3=2;4=1")
data$asianid2 <- car::recode(data$prequestionnaire.1.player.panid2, "1=4;2=3;3=2;4=1")
data$asianids <- data$asianid1+data$asianid2
summary(data$asianids)

min_asianids <- min(data$asianids)
max_asianids <- max(data$asianids)

data$asianids <- ((data$asianids-min_asianids)/(max_asianids-min_asianids))
summary(data$asianids)

data$ethnicid1 <- car::recode(data$prequestionnaire.1.player.subid1, "1=4;2=3;3=2;4=1")
data$ethnicid2 <- car::recode(data$prequestionnaire.1.player.subid2, "1=4;2=3;3=2;4=1")
data$ethnicids <- data$ethnicid1+data$ethnicid2
summary(data$ethnicids)

min_ethnicids <- min(data$ethnicids)
max_ethnicids <- max(data$ethnicids)

data$ethnicids <- ((data$ethnicids-min_ethnicids)/(max_ethnicids-min_ethnicids))
summary(data$ethnicids)

data$Asian_ID_Centered <- data$asianids - mean(data$asianids)
data$Ethnic_ID_Centered <- data$ethnicids - mean(data$ethnicids)

data$pid3<-as.numeric(factor(data$participant.pid))
table(data$pid3)

data$pid7 <- case_when(
  data$pid3==3&data$participant.pidstr==0~1,
  data$pid3==3&data$participant.pidstr==1~2,
  data$pid3==2&data$participant.ilean==2~3,
  data$pid3==2&data$participant.ilean==3~4,
  data$pid3==2&data$participant.ilean==1~5,
  data$pid3==1&data$participant.pidstr==1~6,
  data$pid3==1&data$participant.pidstr==0~7
)

data$dem <- case_when(
  data$pid7>=5~1,
  data$pid7<5~0
)
data$rep <- case_when(
  data$pid7<=3~1,
  data$pid7>3~0
)
data$indep <- case_when(
  data$pid7==4~1,
  data$pid7!=4~0
)
table(data$dem)
table(data$rep)
table(data$indep)
table(data$pid7)
table(data$participant.ideology)
data$liberalism <- case_when(
  data$participant.ideology==7~1,
  data$participant.ideology==6~2,
  data$participant.ideology==5~3,
  data$participant.ideology==4~4,
  data$participant.ideology==3~5,
  data$participant.ideology==2~6,
  data$participant.ideology==1~7
)

data$lib <- case_when(
  data$liberalism>=5~1,
  data$liberalism<5~0
)
data$cons <- case_when(
  data$liberalism<=3~1,
  data$liberalism>3~0
)
data$mod <- case_when(
  data$liberalism==4~1,
  data$liberalism!=4~0
)

table(data$participant.polint)
data$polint<-car::recode(data$participant.polint,"1=4;2=3;3=2;4=1")
data$polint<-(data$polint-1)/(4-1)
table(data$polint)

table(data$prequestionnaire.1.player.educ)
table(data$prequestionnaire.1.player.income)
table(data$participant.age)

data$bachelors<-ifelse(data$prequestionnaire.1.player.educ>=6,1,0)
data$inc <- (data$prequestionnaire.1.player.income-1)/(11-1)
data$age<-data$participant.age

table(data$postquestionnaire5.1.player.nativity)
table(data$prequestionnaire.1.player.sex)

data$nativity <- case_when(
  data$postquestionnaire5.1.player.nativity==1~1,
  data$postquestionnaire5.1.player.nativity==2~0,
  data$postquestionnaire5.1.player.nativity==3~0,
  data$postquestionnaire5.1.player.nativity==4~0
)
data$female <- case_when(
  data$prequestionnaire.1.player.sex==1~0,
  data$prequestionnaire.1.player.sex==2~1
)

data$treatment<-factor(data$participant.treatment, levels=c(0,1),labels=c("Control","Treatment"))
data$treatment_num<-data$participant.treatment
#table(data$treatment_num)

data$angry <- case_when(
  data$vignette.1.player.angry==1~4,
  data$vignette.1.player.angry==2~3,
  data$vignette.1.player.angry==3~2,
  data$vignette.1.player.angry==4~1
)

data$hostile <- case_when(
  data$vignette.1.player.hostile==1~4,
  data$vignette.1.player.hostile==2~3,
  data$vignette.1.player.hostile==3~2,
  data$vignette.1.player.hostile==4~1
)

data$disgusted <- case_when(
  data$vignette.1.player.disgusted==1~4,
  data$vignette.1.player.disgusted==2~3,
  data$vignette.1.player.disgusted==3~2,
  data$vignette.1.player.disgusted==4~1
)

data$enthusiastic <- case_when(
  data$vignette.1.player.enthusiastic==1~4,
  data$vignette.1.player.enthusiastic==2~3,
  data$vignette.1.player.enthusiastic==3~2,
  data$vignette.1.player.enthusiastic==4~1
)

data$hopeful <- case_when(
  data$vignette.1.player.hopeful==1~4,
  data$vignette.1.player.hopeful==2~3,
  data$vignette.1.player.hopeful==3~2,
  data$vignette.1.player.hopeful==4~1
)

data$proud <- case_when(
  data$vignette.1.player.proud==1~4,
  data$vignette.1.player.proud==2~3,
  data$vignette.1.player.proud==3~2,
  data$vignette.1.player.proud==4~1
)

data$cq1_correct <- ifelse(data$instruction.1.player.cq1_correct==0,1,0)
data$cq2_correct <- ifelse(data$instruction.1.player.cq2_correct==0,1,0)
data$cq3_correct <- ifelse(data$instruction.1.player.cq3_correct==0,1,0)
data$cq4_correct <- ifelse(data$instruction.1.player.cq4_correct==0,1,0)

data$cq_score <- (data$cq1_correct+data$cq2_correct+data$cq3_correct+data$cq4_correct)
table(data$cq_score)
data$cq_score <- ((data$cq_score-0)
                  /(4-0))
table(data$cq_score)

data$fullcomp <- ifelse(data$instruction.1.player.cq1_correct==0
                        &data$instruction.1.player.cq2_correct==0
                        &data$instruction.1.player.cq3_correct==0
                        &data$instruction.1.player.cq4_correct==0,1,0)
table(data$fullcomp)

data$failed_too_many<-data$participant.failed_too_many

data$self_contribution<-data$pgg.1.player.contribution
data$self_contributionperc<-(data$self_contribution/150)*100
data$expamt_partner<-data$pgg.1.player.expamt_partner
data$expperc_partner<-(data$expamt_partner/150)*100
data$dyadicntl<-factor(data$pgg.1.group.dyadicntl, levels=c(0,1),labels=c("Out-Ethnic","Co-Ethnic"))
data$dyadicntl_num<-as.numeric(data$dyadicntl)
data$dyadicntl_num<-car::recode(data$dyadicntl_num,"1=0;2=1")
table(data$dyadicntl_num)

data$TmtNtl<-case_when(
  data$participant.treatment==0&data$pgg.1.group.dyadicntl==0~1,
  data$participant.treatment==1&data$pgg.1.group.dyadicntl==0~2,
  data$participant.treatment==0&data$pgg.1.group.dyadicntl==1~3,
  data$participant.treatment==1&data$pgg.1.group.dyadicntl==1~4
)
data$TmtNtl<-factor(data$TmtNtl, levels=c(1,2,3,4),labels=c("Control:Out-Ethnic","Treatment:Out-Ethnic","Control:Co-Ethnic","Treatment:Co-Ethnic"))
data$TmtNtl1<-data$TmtNtl
table(data$TmtNtl1)
data$TmtNtl1<-as.numeric(data$TmtNtl1)
data$TmtNtl1<-car::recode(data$TmtNtl1,"1=2;2=4;3=1;4=3")
data$TmtNtl1<-factor(data$TmtNtl1,levels=c(1,2,3,4),labels=c("Control:Co-Ethnic","Control:Out-Ethnic","Treatment:Co-Ethnic","Treatment:Out-Ethnic"))

data$bot_matched<-factor(data$participant.bot_matched, levels=c(0,1),labels=c("Matched Partner = Human","Matched Partner = Bot"))
data$bot_matched_num<-as.numeric(data$bot_matched)
table(data$bot_matched_num)
data$bot_matched_num<-car::recode(data$bot_matched_num,"1=0;2=1")
data$bot_contribution<-data$pgg.1.group.bp_contribution

data$outgroup_imm1 <- case_when(
  data$participant.sasian==0&data$postquestionnaire1.1.player.india==1~5,
  data$participant.sasian==0&data$postquestionnaire1.1.player.india==2~4,
  data$participant.sasian==0&data$postquestionnaire1.1.player.india==3~3,
  data$participant.sasian==0&data$postquestionnaire1.1.player.india==4~2,
  data$participant.sasian==0&data$postquestionnaire1.1.player.india==5~1,
  data$participant.sasian==1&data$postquestionnaire1.1.player.china==1~5,
  data$participant.sasian==1&data$postquestionnaire1.1.player.china==2~4,
  data$participant.sasian==1&data$postquestionnaire1.1.player.china==3~3,
  data$participant.sasian==1&data$postquestionnaire1.1.player.china==4~2,
  data$participant.sasian==1&data$postquestionnaire1.1.player.china==5~1
)
data$outgroup_imm2 <- case_when(
  data$participant.sasian==0&data$postquestionnaire1.1.player.pakistan==1~5,
  data$participant.sasian==0&data$postquestionnaire1.1.player.pakistan==2~4,
  data$participant.sasian==0&data$postquestionnaire1.1.player.pakistan==3~3,
  data$participant.sasian==0&data$postquestionnaire1.1.player.pakistan==4~2,
  data$participant.sasian==0&data$postquestionnaire1.1.player.pakistan==5~1,
  data$participant.sasian==1&data$postquestionnaire1.1.player.korea==1~5,
  data$participant.sasian==1&data$postquestionnaire1.1.player.korea==2~4,
  data$participant.sasian==1&data$postquestionnaire1.1.player.korea==3~3,
  data$participant.sasian==1&data$postquestionnaire1.1.player.korea==4~2,
  data$participant.sasian==1&data$postquestionnaire1.1.player.korea==5~1
)

data$outgroup_imm <- (data$outgroup_imm1+data$outgroup_imm2)
summary(data$outgroup_imm)
data$outgroup_imm <- (data$outgroup_imm-2)/(10-2)
summary(data$outgroup_imm)
table(data$outgroup_imm)

table(data$outgroup_imm1)
data$outgroup_imm1<-(data$outgroup_imm1-1)/(5-1)

table(data$outgroup_imm2)
data$outgroup_imm2<-(data$outgroup_imm2-1)/(5-1)

data$selfntl_imm <- case_when(
  data$postquestionnaire1.1.player.selfntl==1~5,
  data$postquestionnaire1.1.player.selfntl==2~4,
  data$postquestionnaire1.1.player.selfntl==3~3,
  data$postquestionnaire1.1.player.selfntl==4~2,
  data$postquestionnaire1.1.player.selfntl==5~1
)
table(data$selfntl_imm)
data$selfntl_imm<-(data$selfntl_imm-1)/(5-1)
table(data$selfntl_imm)

data$france <- case_when(
  data$postquestionnaire1.1.player.france==1~5,
  data$postquestionnaire1.1.player.france==2~4,
  data$postquestionnaire1.1.player.france==3~3,
  data$postquestionnaire1.1.player.france==4~2,
  data$postquestionnaire1.1.player.france==5~1)
table(data$france)
data$france<-(data$france-1)/(5-1)
table(data$france)

data$germany <- case_when(
  data$postquestionnaire1.1.player.germany==1~5,
  data$postquestionnaire1.1.player.germany==2~4,
  data$postquestionnaire1.1.player.germany==3~3,
  data$postquestionnaire1.1.player.germany==4~2,
  data$postquestionnaire1.1.player.germany==5~1)
table(data$germany)
data$germany<-(data$germany-1)/(5-1)
table(data$germany)

data$mexico <- case_when(
  data$postquestionnaire1.1.player.mexico==1~5,
  data$postquestionnaire1.1.player.mexico==2~4,
  data$postquestionnaire1.1.player.mexico==3~3,
  data$postquestionnaire1.1.player.mexico==4~2,
  data$postquestionnaire1.1.player.mexico==5~1)
table(data$mexico)
data$mexico<-(data$mexico-1)/(5-1)
table(data$mexico)

data$ukraine <- case_when(
  data$postquestionnaire1.1.player.ukraine==1~5,
  data$postquestionnaire1.1.player.ukraine==2~4,
  data$postquestionnaire1.1.player.ukraine==3~3,
  data$postquestionnaire1.1.player.ukraine==4~2,
  data$postquestionnaire1.1.player.ukraine==5~1)
table(data$ukraine)
data$ukraine<-(data$ukraine-1)/(5-1)
table(data$ukraine)

#clean data 
data1<-data[c(
  "sasian",
  "easian",
  "seasian",
  "chinese",
  "indian",
  "vietnamese",
  "korean",
  "filipino",
  "japanese",
  "allothers",
  "asianids",
  "ethnicids",
  "Asian_ID_Centered",
  "Ethnic_ID_Centered",
  "pid7",
  "dem",
  "rep",
  "indep",
  "liberalism",
  "lib",
  "cons",
  "mod",
  "polint",
  "bachelors",
  "inc",
  "age",
  "nativity",
  "female",
  "treatment",
  "treatment_num",
  "angry",
  "hostile",
  "disgusted",
  "enthusiastic",
  "hopeful",
  "proud",
  "cq_score",
  "fullcomp",
  "failed_too_many",
  "self_contribution",
  "self_contributionperc",
  "expamt_partner",
  "expperc_partner",
  "dyadicntl",
  "dyadicntl_num",
  "TmtNtl",
  "TmtNtl1",
  "bot_matched",
  "bot_matched_num",
  "bot_contribution",
  "outgroup_imm1",
  "outgroup_imm2",
  "outgroup_imm",
  "selfntl_imm",
  "france",
  "germany",
  "mexico",
  "ukraine"
)]


write_rds(data1,"sk_pobe_cleandata.rds")